<?php

class BMode_cache extends BMode
{
    
    /**
     * 缓存信号：缓存过期
     */
    const SIGNAL_EXPIRE = 0;
    
    /**
     * 缓存信号：缓存正常
     */
    const SIGNAL_NORMAL = 1;
    
    /**
     * 支持的缓存引擎
     */
    private $_engines = array('memcached', 'file', 'tyrant', 'static');
    
    /**
     * 当前数据库引擎
     */
    private $_engine = 'memcached';
    
    /**
     * 缓存信号
     *
     * @access private
     * @var int
     */
    protected $_signal = self::SIGNAL_NORMAL;
    
    /**
     * 设置缓存引擎
     *
     * @param $engine string           
     * @return mixed
     */
    function engine($engine = '')
    {
        if ($engine&&in_array($engine, $this->_engines)) {
            $this->_engine = $engine;
        } else {
            return $this->_engine;
        }
        return $this;
    }
    
    /**
     * 设置缓存信号
     *
     * @access public
     * @param $signal int           
     * @return mixed
     */
    function signal($signal = null)
    {
        if (is_null($signal)) {
            return $this->_signal;
        }
        $this->_signal = $signal;
        return $this;
    }
    
    /**
     * 取得已设置的缓存器实例
     *
     * @access public
     * @param $drivername string           
     * @return object
     */
    function pick()
    {
        return $this->_getInstance($cache);
    }
    
    /**
     * 获取缓存实例
     *
     * @access private
     * @param $engine string           
     * @param $database string           
     * @return mixed
     */
    private function _getInstance()
    {
        $class = BLoader::instance("../Mode_cache_{$this->_engine}");
        if ($instance = $class->connect($this->_engine)) {
            return $instance;
        } else {
            throw new BCacheException('engine_unsupported', $this->_engine);
        }
    }
}

// 缓存引擎接口
interface DICache
{
    function connect($link);
    //
    function valid();
    
    function instance();
    
    function expire($time);
    
    function set($name, $data);
    
    function get($name);
    
    function delete($name);

}

?>